# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=exiv2

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.24
pkgrel=2
pkgdesc="Exif and Iptc metadata manipulation library and tools (mingw-w64)"
arch=('any')
url="http://exiv2.org"
license=("GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
    "${MINGW_PACKAGE_PREFIX}-gettext"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libtool"
    )
options=('strip' 'staticlibs')
source=("http://www.exiv2.org/${_realname}-${pkgver}.tar.gz"
    0001-fix-cr-for-awk-in-configure.mingw.patch)
sha1sums=('2f19538e54f8c21c180fa96d17677b7cff7dc1bb'
          '4c9dca6f5517ba5c857162441a28edd68b4bdeb5')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-fix-cr-for-awk-in-configure.mingw.patch
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  CPPFLAGS+=" -D_FILE_OFFSET_BITS=64"
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --disable-static

  # Fix executables extensions
  sed -i 's|BINARY = $(BINSRC:.cpp=)|BINARY = $(BINSRC:.cpp=$(EXEEXT))|;
    s|EXIV2BIN = $(EXIV2MAIN:.cpp=)|EXIV2BIN = $(EXIV2MAIN:.cpp=$(EXEEXT))|;
    s|MCBIN = $(MCMAIN:.cpp=)|MCBIN = $(MCMAIN:.cpp=$(EXEEXT))|;
    s|-o $@ $(LIBRARY) $@.o|-o $@ $(LIBRARY) $(BINOBJ)|
    s|$(BINARY): %: %.o lib|$(BINARY): lib $(BINOBJ)|' src/Makefile

}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make || true
  cp src/.libs/localtime.o src/
  make DESTDIR="$pkgdir" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
